package com.example.hao.animation_demo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.res.Resources;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

public class V6_upgrade_Activity extends AppCompatActivity {

    private static final String TAG = "V6_upgrade_Activity";
    private View iv_star;
    int width;
    int height;
    private View iv_start1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_v6_upgrade_);
        initView();
    }

    private void initView() {
        iv_star = findViewById(R.id.iv_star);
        iv_start1 = findViewById(R.id.iv_start1);
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        float density = dm.density;
        width = dm.widthPixels;//1080
        height = dm.heightPixels;//1902

        //x^2+bx+height;

        Log.d(TAG, "onCreate: " + width + "," + height);
        ObjectAnimator a1 = ObjectAnimator.ofFloat(iv_star, "X", width - 240, 0).setDuration(3000);
        ObjectAnimator a2 = ObjectAnimator.ofFloat(iv_star, "Y", 0, height - 240).setDuration(3000);
        //这种实现比较抽象，前提是必须先理解速率变换给动画运行所带来的影响，比如说当速率是负数的时候，会改变动画的运动方向。
        a2.setInterpolator((new AnticipateInterpolator(-0.8f)));
        ObjectAnimator a3 = ObjectAnimator.ofFloat(iv_star, "Alpha", 1, 0.1f).setDuration(3000);
        ObjectAnimator a4 = ObjectAnimator.ofFloat(iv_star, "scaleX", 2.0f, 0.1f).setDuration(3000);
        ObjectAnimator a5 = ObjectAnimator.ofFloat(iv_star, "scaleY", 2.0f, 0.1f).setDuration(3000);
        ObjectAnimator a6 = ObjectAnimator.ofFloat(iv_star, "rotation", 0, 720).setDuration(3000);
        AnimatorSet animatorSet = new AnimatorSet();

        animatorSet.playTogether(a1,a2,a3,a4,a5,a6);
//        animatorSet.play(a1).with(a2).with(a3).with(a4).with(a5);
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                iv_start1.setVisibility(View.VISIBLE);
                anim = animation;
            }
//            @Override
//            public void onAnimationStart(Animator animation) {
//                iv_start1.setVisibility(View.INVISIBLE);
//            }
        });
        animatorSet.start();
    }

    Animator anim;
    public void star2run(View view) {
        iv_start1.setVisibility(View.INVISIBLE);
        anim.start();
    }
}
